package main

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
	"time"
)

type MyHook struct {
}

// Levels 过滤要处理的等级
func (h MyHook) Levels() []logrus.Level {
	return []logrus.Level{logrus.ErrorLevel}
}

// Fire 处理逻辑
func (h MyHook) Fire(entry *logrus.Entry) error {
	fmt.Println(*entry)
	entry.Data["app"] = "MyApp"
	file, err := os.OpenFile("./src/03_gin/12_log/logrus/2_hook/error.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		return err
	}
	defer file.Close()
	line, err := entry.String()
	if err != nil {
		return err
	}
	_, err = file.Write([]byte(line))
	return err
}

func main() {
	//logrus.SetReportCaller(true)
	logrus.SetFormatter(&logrus.TextFormatter{
		ForceColors:            false,
		FullTimestamp:          true,
		TimestampFormat:        time.DateTime,
		DisableSorting:         true,
		DisableLevelTruncation: true,
	})
	logrus.AddHook(&MyHook{})
	logrus.Warnln("你好")
	logrus.Errorln("你好")
}
